import gamestate
import os, pygame

class GameOver(gamestate.GameState):
    """GameState for the game being over and showing a recap"""
    
    def __init__(self):
        #basic keybinds
        self.keybinds = {'select_down': pygame.K_DOWN,
                         'select_up': pygame.K_UP,
                         'advance': pygame.K_RETURN}
        
        #link actions to appropriate function references
        self._actions = {'select_down': self._action_select_down,
                         'select_up': self._action_select_up,
                         'advance': self._action_advance}
        
        self._newState = self
        
        #gamestates that can be selected from menu
        self._options = ['mainmenu', 'credits']
        self._selected = 0
        
        self._prep_listing()
        self._load_images()
        
    def draw(self, screen):
        screen.blit(self._bg, (0, 0))
        
        for x in range(len(self._options)):
            offon = (1 if self._selected==x else 0)
            screen.blit(self._listing[x][offon], (200, x*60+100))
        
    def enter_state(self):
        self._newState = self
        self._selected = 0
        
    def update(self, etime):
        return self._newState
        
    def _action_advance(self):
        #confirm that a valid state exists for current selection
        if self._options[self._selected] in gamestate.GameState.validStates:
            self._newState = gamestate.GameState.validStates[
                                                self._options[self._selected]]
    
    def _action_select_down(self):
        #adjust to 1 option higher (bound by size of options list)
        self._selected += 1
        self._selected %= len(self._options)
    
    def _action_select_up(self):
        #adjust to 1 option lower (bound about zero)
        self._selected -= 1
        if self._selected < 0:
            self._selected += len(self._options)
            
    def _load_images(self):
        self._bg = pygame.image.load(os.path.join('imgs', 'to-cont-notext.png'))
        self._listing[0].append(pygame.image.load(os.path.join('imgs', 'go-to-menu-off.png')))
        self._listing[0].append(pygame.image.load(os.path.join('imgs', 'go-to-menu-on.png')))
        self._listing[1].append(pygame.image.load(os.path.join('imgs', 'credits-off.png')))
        self._listing[1].append(pygame.image.load(os.path.join('imgs', 'credits-on.png')))
        
    def _prep_listing(self):
        self._listing = []
        for x in range(len(self._options)):
            self._listing.append([])